package com.boot.shiro.controller;

import com.boot.shiro.result.Result;
import com.boot.shiro.util.ShiroUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SysLoginController {
    private static Logger LOGGER = LoggerFactory.getLogger(ShiroController.class) ;

    @RequestMapping("/sys/login")
    @ResponseBody
    public Result userLogin (String username, String password){
        try{
            Subject subject = ShiroUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            subject.login(token);
            LOGGER.info("登录成功");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return Result.success(null);
    }

    /**
     * 退出
     */
    @RequestMapping(value = "logout", method = RequestMethod.GET)
    public String logout() {
        ShiroUtils.logout();
        return "redirect:login.html";
    }
}
